package com.example.mrzhou.lvmm.view.fragment.tm;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.example.mrzhou.lvmm.R;
import com.example.mrzhou.lvmm.commom.base.BaseFragment;
import com.example.mrzhou.lvmm.entity.tm.TM;
import com.example.mrzhou.lvmm.entity.tm.TMSendMsg;
import com.example.mrzhou.lvmm.presenter.tm.TMPresenter;
import com.example.mrzhou.lvmm.view.adapter.tm.TMAdapter;
import com.example.mrzhou.lvmm.view.impl.tm.TMView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class TMSampleFragment extends BaseFragment implements TMView {

    public static final String KEY = "key";
    @BindView(R.id.xclv_tm)
    XRecyclerView xclvTm;
    private List<TM.DataBean.GroupbuyListBean> data = new ArrayList<>();
    private TMAdapter adapter;
    private TMSendMsg msg;

    public TMSampleFragment() {
        // Required empty public constructor
    }

    public static TMSampleFragment newInstance(TMSendMsg msg) {

        Bundle args = new Bundle();
        args.putSerializable(KEY, msg);
        TMSampleFragment fragment = new TMSampleFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_tmsample;
    }

    @Override
    protected void initView(View view) {
        ButterKnife.bind(this, view);
        xclvTm.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new TMAdapter(data, getContext());
        xclvTm.setAdapter(adapter);
        xclvTm.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initData();
            }

            @Override
            public void onLoadMore() {

            }
        });
        xclvTm.setLoadingMoreEnabled(false);
        xclvTm.setRefreshing(true);
    }

    @Override
    protected void initData() {
        msg = (TMSendMsg) getArguments().getSerializable(KEY);

        TMPresenter presenter = new TMPresenter(this);

        Map<String, String> map = new HashMap<>();
//        map.put("method", "api.com.groupbuy.getWeekHotCollections");
        map.put("method", msg.getMethod());
        map.put("globalLatitude", "39.913248");
        map.put("globalLongitude", "116.403624");
//        map.put("clientTimestamp", "1477723294600");
        map.put("clientTimestamp", msg.getClientTimestamp());
//        map.put("lvtukey", "1d929d6e2fd6ce132f72112ab8eac088");
        map.put("lvtukey", msg.getLvtukey());
        map.put("osVersion", "4.4.2");
        map.put("lvversionCode", "65");
        map.put("lvversion", "7.7.3");
        map.put("deviceName", "V1.0");
        map.put("firstChannel", "ANDROID");
        map.put("udid", "864394010807818");
        map.put("formate", "json");
        map.put("secondChannel", "YYB");
//        Map<String, String> params = new HashMap<>();
//        params.put("page", "1");
//        params.put("pageSize", "7");
//        params.put("stationName", "上海");
//        params.put("stationCode", "SH");
//        params.put("version", "1.0.0");
        map.put("page", "1");
        map.put("pageSize", "7");
        map.put("stationName", "上海");
        map.put("stationCode", "SH");
        map.put("version", "1.0.0");
        switch (msg.getIndex()) {
            case 0:
                break;
            case 1:
                map.put("productMark", msg.getProductMark());
                map.put("seckillType", msg.getSeckillType());
                break;
            case 2:
                map.put("productMark", msg.getProductMark());
                map.put("seckillType", msg.getSeckillType());
                break;
        }
//        presenter.getTMData(map,params);
        presenter.getTMData(map);
    }


    @Override
    public void onSuccess(TM tm) {
        if (tm != null) {
            data.clear();
            data.addAll(tm.getData().getGroupbuyList());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFaiure(Throwable a) {

    }

    @Override
    public void onFinsh() {
        xclvTm.refreshComplete();
    }
}
